# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause

cmake_minimum_required(VERSION ${QT_CONFIGURE_CHECK_cmake_version})

find_package(Ninja ${QT_CONFIGURE_CHECK_ninja_version} REQUIRED)
find_package(Nodejs ${QT_CONFIGURE_CHECK_nodejs_version} REQUIRED)
find_package(Perl)
find_package(PkgConfig)
if(PkgConfig_FOUND)
    create_pkg_config_host_wrapper(${CMAKE_CURRENT_BINARY_DIR})
endif()

set(buildDir "${CMAKE_CURRENT_BINARY_DIR}")

add_subdirectory(api)
add_subdirectory(tools/webenginedriver)
add_subdirectory(tools/qwebengine_convert_dict)

##
#   TOOLCHAIN SETUP
##

if(LINUX OR WIN32)
    setup_toolchains()
endif()

##
#   GN BUILD SETUP
##

addSyncTargets(core)

get_configs(configs)
get_architectures(archs)

foreach(arch ${archs})
    foreach(config ${configs})

        ##
        # BUILD.gn SETUP
        ##

        set(buildGn core_${config}_${arch})
        add_gn_target(${buildGn} ${config} ${arch}
            SOURCES DEFINES CXX_COMPILE_OPTIONS C_COMPILE_OPTIONS INCLUDES MOC_PATH
        )
        resolve_target_includes(gnIncludes WebEngineCore)
        get_forward_declaration_macro(forwardDeclarationMacro)
        get_target_property(qtWebEngineProcessName WebEngineCore QTWEBENGINEPROCESS_NAME)

        if("${config}" STREQUAL "Debug")
            set(qtWebEngineProcessName "${qtWebEngineProcessName}${CMAKE_DEBUG_POSTFIX}")
        endif()

        set(gnCxxCompileOptions "")
        if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
            get_gn_arch(cpu ${TEST_architecture_arch})

            # Silence warning in boringssl headers.
            list(APPEND gnCxxCompileOptions -Wno-ignored-qualifiers)

            if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "11.0.0" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11.2.0")
                # GCC 11.1 has a regression in the integrated preprocessor, so disable it as a workaround (QTBUG-93360)
                # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100796
                list(APPEND gnCxxCompileOptions -no-integrated-cpp)
            endif()

            if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL "15.1.0" AND cpu STREQUAL "arm")
                # GCC 15.1 workaround for invalid use of NEON registers,
                # see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120351
                list(APPEND gnCxxCompileOptions -fno-late-combine-instructions)
            endif()
        endif()

        extend_gn_target(${buildGn}
            INCLUDES
                ${gnIncludes}
            DEFINES
                QT_NO_KEYWORDS
                QT_USE_QSTRINGBUILDER
                QTWEBENGINECORE_VERSION_STR=${QT_REPO_MODULE_VERSION}
                QTWEBENGINEPROCESS_NAME=${qtWebEngineProcessName}
                BUILDING_CHROMIUM
                "${forwardDeclarationMacro}"
            CXX_COMPILE_OPTIONS
                ${gnCxxCompileOptions}
            SOURCES
                accessibility_tree_formatter_qt.cpp
                browser_accessibility_qt.cpp browser_accessibility_qt.h
                authentication_dialog_controller.cpp authentication_dialog_controller.h authentication_dialog_controller_p.h
                autofill_client_qt.cpp autofill_client_qt.h
                autofill_popup_controller.cpp autofill_popup_controller.h autofill_popup_controller_p.h
                browser_accessibility_manager_qt.cpp browser_accessibility_manager_qt.h
                browser_main_parts_qt.cpp browser_main_parts_qt.h
                browsing_data_remover_delegate_qt.cpp browsing_data_remover_delegate_qt.h
                build_config_qt.h
                certificate_error_controller.cpp certificate_error_controller.h
                chromium_overrides.cpp
                client_cert_select_controller.cpp client_cert_select_controller.h
                client_hints.cpp client_hints.h
                clipboard_change_observer.h
                clipboard_qt.cpp clipboard_qt.h
                color_chooser_controller.cpp color_chooser_controller.h color_chooser_controller_p.h
                color_chooser_qt.cpp color_chooser_qt.h
                compositor/compositor.cpp compositor/compositor.h
                compositor/display_overrides.cpp
                compositor/display_software_output_surface.cpp compositor/display_software_output_surface.h
                compositor/native_skia_output_device.cpp compositor/native_skia_output_device.h
                content_browser_client_qt.cpp content_browser_client_qt.h
                content_client_qt.cpp content_client_qt.h
                content_main_delegate_qt.cpp content_main_delegate_qt.h
                content_utility_client_qt.cpp content_utility_client_qt.h
                custom_handlers/protocol_handler_registry_delegate_qt.h custom_handlers/protocol_handler_registry_delegate_qt.cpp
                custom_handlers/protocol_handler_registry_factory.h custom_handlers/protocol_handler_registry_factory.cpp
                custom_handlers/register_protocol_handler_request_controller.h
                custom_handlers/register_protocol_handler_request_controller_impl.cpp custom_handlers/register_protocol_handler_request_controller_impl.h
                delegated_frame_host_client_qt.cpp delegated_frame_host_client_qt.h
                desktop_media_controller.cpp desktop_media_controller.h desktop_media_controller_p.h
                desktop_screen_qt.cpp desktop_screen_qt.h
                devtools_frontend_qt.cpp devtools_frontend_qt.h
                devtools_manager_delegate_qt.cpp devtools_manager_delegate_qt.h
                download_manager_delegate_qt.cpp download_manager_delegate_qt.h
                favicon_driver_qt.cpp favicon_driver_qt.h
                favicon_service_factory_qt.cpp favicon_service_factory_qt.h
                file_picker_controller.cpp file_picker_controller.h
                file_system_access/file_system_access_permission_context_factory_qt.cpp file_system_access/file_system_access_permission_context_factory_qt.h
                file_system_access/file_system_access_permission_context_qt.cpp file_system_access/file_system_access_permission_context_qt.h
                file_system_access/file_system_access_permission_grant_qt.cpp file_system_access/file_system_access_permission_grant_qt.h
                file_system_access/file_system_access_permission_request_controller.h
                file_system_access/file_system_access_permission_request_controller_impl.cpp file_system_access/file_system_access_permission_request_controller_impl.h
                file_system_access/file_system_access_permission_request_manager_qt.cpp file_system_access/file_system_access_permission_request_manager_qt.h
                find_text_helper.cpp find_text_helper.h
                global_descriptors_qt.h
                gpu/content_gpu_client_qt.cpp gpu/content_gpu_client_qt.h
                javascript_dialog_controller.cpp javascript_dialog_controller.h javascript_dialog_controller_p.h
                javascript_dialog_manager_qt.cpp javascript_dialog_manager_qt.h
                login_delegate_qt.cpp login_delegate_qt.h
                media_capture_devices_dispatcher.cpp media_capture_devices_dispatcher.h
                native_web_keyboard_event_qt.cpp native_web_keyboard_event_qt.h
                net/client_cert_qt.cpp net/client_cert_qt.h
                net/client_cert_store_data.cpp net/client_cert_store_data.h
                net/cookie_monster_delegate_qt.cpp net/cookie_monster_delegate_qt.h
                net/custom_url_loader_factory.cpp net/custom_url_loader_factory.h
                net/proxy_config_monitor.cpp net/proxy_config_monitor.h
                net/proxy_config_service_qt.cpp net/proxy_config_service_qt.h
                net/proxying_restricted_cookie_manager_qt.cpp net/proxying_restricted_cookie_manager_qt.h
                net/proxying_url_loader_factory_qt.cpp net/proxying_url_loader_factory_qt.h
                net/qrc_url_scheme_handler.cpp net/qrc_url_scheme_handler.h
                net/resource_request_body_qt.cpp net/resource_request_body_qt.h
                net/ssl_host_state_delegate_qt.cpp net/ssl_host_state_delegate_qt.h
                net/system_network_context_manager.cpp net/system_network_context_manager.h
                net/url_request_custom_job_delegate.cpp net/url_request_custom_job_delegate.h
                net/url_request_custom_job_proxy.cpp net/url_request_custom_job_proxy.h
                net/version_ui_qt.cpp net/version_ui_qt.h
                net/webui_controller_factory_qt.cpp net/webui_controller_factory_qt.h
                permission_manager_qt.cpp permission_manager_qt.h
                pdf_util_qt.cpp pdf_util_qt.h
                platform_notification_service_qt.cpp platform_notification_service_qt.h
                pointer_device_qt.cpp
                pref_service_adapter.cpp pref_service_adapter.h
                process_main.cpp
                profile_adapter.cpp profile_adapter.h
                profile_adapter_client.cpp profile_adapter_client.h
                profile_io_data_qt.cpp profile_io_data_qt.h
                profile_qt.cpp profile_qt.h
                render_view_context_menu_qt.cpp render_view_context_menu_qt.h
                render_widget_host_view_qt.cpp render_widget_host_view_qt.h
                render_widget_host_view_qt_delegate.h
                render_widget_host_view_qt_delegate_client.cpp render_widget_host_view_qt_delegate_client.h
                render_widget_host_view_qt_delegate_item.cpp render_widget_host_view_qt_delegate_item.h
                renderer/content_renderer_client_qt.cpp renderer/content_renderer_client_qt.h
                renderer/content_settings_observer_qt.cpp renderer/content_settings_observer_qt.h
                renderer/render_configuration.cpp renderer/render_configuration.h
                renderer/render_frame_observer_qt.cpp renderer/render_frame_observer_qt.h
                renderer/user_resource_controller.cpp renderer/user_resource_controller.h
                renderer/web_engine_page_render_frame.cpp renderer/web_engine_page_render_frame.h
                renderer_host/user_resource_controller_host.cpp renderer_host/user_resource_controller_host.h
                renderer_host/web_engine_page_host.cpp renderer_host/web_engine_page_host.h
                request_controller.h
                resource_bundle_qt.cpp
                select_file_dialog_factory_qt.cpp select_file_dialog_factory_qt.h
                touch_handle_drawable_client.h
                touch_handle_drawable_qt.cpp touch_handle_drawable_qt.h
                touch_selection_controller_client_qt.cpp touch_selection_controller_client_qt.h
                touch_selection_menu_controller.cpp touch_selection_menu_controller.h
                type_conversion.cpp type_conversion.h
                user_notification_controller.cpp user_notification_controller.h
                user_script.cpp user_script.h
                visited_links_manager_qt.cpp visited_links_manager_qt.h
                web_contents_adapter.cpp web_contents_adapter.h
                web_contents_adapter_client.h
                web_contents_delegate_qt.cpp web_contents_delegate_qt.h
                web_contents_view_qt.cpp web_contents_view_qt.h
                web_engine_context.cpp web_engine_context.h
                web_engine_error.cpp web_engine_error.h
                web_engine_library_info.cpp web_engine_library_info.h
                web_engine_settings.cpp web_engine_settings.h
                web_event_factory.cpp web_event_factory.h
                web_usb_detector_qt.cpp web_usb_detector_qt.h
                authenticator_request_client_delegate_qt.cpp authenticator_request_client_delegate_qt.h
                authenticator_request_dialog_controller.cpp authenticator_request_dialog_controller.h authenticator_request_dialog_controller_p.h
                content_settings_manager_qt.cpp content_settings_manager_qt.h
        )

        extend_gn_target(${buildGn} CONDITION config STREQUAL "Debug"
            DEFINES
                QT_USE_DEBUG_RESOURCES
        )

        extend_gn_target(${buildGn} CONDITION QT_FEATURE_accessibility
            SOURCES
                accessibility_activation_observer.cpp accessibility_activation_observer.h
        )

        extend_gn_target(${buildGn} CONDITION LINUX
            SOURCES
                ozone/gl_ozone_qt.cpp ozone/gl_ozone_qt.h
                ozone/ozone_util_qt.cpp ozone/ozone_util_qt.h
                ozone/platform_window_qt.cpp ozone/platform_window_qt.h
                ozone/surface_factory_qt.cpp ozone/surface_factory_qt.h
        )

        extend_gn_target(${buildGn} CONDITION LINUX AND QT_FEATURE_opengl
            SOURCES
                ozone/gl_helper.cpp ozone/gl_helper.h
        )

        extend_gn_target(${buildGn} CONDITION LINUX AND QT_FEATURE_opengl AND QT_FEATURE_webengine_ozone_x11 AND QT_FEATURE_xcb_glx_plugin
            SOURCES
                ozone/glx_helper.cpp ozone/glx_helper.h
        )

        extend_gn_target(${buildGn} CONDITION LINUX AND QT_FEATURE_opengl AND QT_FEATURE_egl
            SOURCES
                ozone/egl_helper.cpp ozone/egl_helper.h
        )

        extend_gn_target(${buildGn} CONDITION QT_FEATURE_opengl
            SOURCES
                compositor/native_skia_output_device_opengl.cpp compositor/native_skia_output_device_opengl.h
        )

        extend_gn_target(${buildGn} CONDITION QT_FEATURE_webengine_vulkan
            SOURCES
                compositor/native_skia_output_device_vulkan.cpp compositor/native_skia_output_device_vulkan.h
                compositor/vulkan_implementation_qt.cpp compositor/vulkan_implementation_qt.h
        )

        extend_gn_target(${buildGn} CONDITION MACOS
            SOURCES
                native_web_keyboard_event_qt_mac.mm
                compositor/native_skia_output_device_mac.mm
                compositor/native_skia_output_device_metal.cpp compositor/native_skia_output_device_metal.h
                cursor_utils_qt_mac.mm cursor_utils_qt_mac.h
        )

        extend_gn_target(${buildGn} CONDITION QT_FEATURE_webengine_printing_and_pdf
            SOURCES
                printing/pdfium_document_wrapper_qt.cpp printing/pdfium_document_wrapper_qt.h
                printing/print_view_manager_base_qt.cpp printing/print_view_manager_base_qt.h
                printing/print_view_manager_qt.cpp printing/print_view_manager_qt.h
                printing/printer_worker.cpp printing/printer_worker.h
                renderer/print_web_view_helper_delegate_qt.cpp renderer/print_web_view_helper_delegate_qt.h
        )

        extend_gn_target(${buildGn} CONDITION QT_FEATURE_webengine_geolocation
            SOURCES
                location_provider_qt.cpp location_provider_qt.h
        )

        extend_gn_target(${buildGn} CONDITION QT_FEATURE_webengine_webchannel
            SOURCES
                renderer/web_channel_ipc_transport.cpp renderer/web_channel_ipc_transport.h
                renderer_host/web_channel_ipc_transport_host.cpp renderer_host/web_channel_ipc_transport_host.h
        )

        extend_gn_target(${buildGn} CONDITION QT_FEATURE_webengine_extensions
            SOURCES
                common/extensions/extensions_api_provider_qt.cpp common/extensions/extensions_api_provider_qt.h
                common/extensions/extensions_client_qt.cpp common/extensions/extensions_client_qt.h
                extensions/api/display_info_provider_qt.cpp extensions/api/display_info_provider_qt.h
                extensions/api/runtime_api_delegate_qt.cpp extensions/api/runtime_api_delegate_qt.h
                extensions/api/management_api_delegate_qt.cpp extensions/api/management_api_delegate_qt.h
                extensions/component_extension_resource_manager_qt.cpp extensions/component_extension_resource_manager_qt.h
                extensions/extension_host_delegate_qt.cpp extensions/extension_host_delegate_qt.h
                extensions/extension_system_factory_qt.cpp extensions/extension_system_factory_qt.h
                extensions/extension_system_qt.cpp extensions/extension_system_qt.h
                extensions/extension_web_contents_observer_qt.cpp extensions/extension_web_contents_observer_qt.h
                extensions/extensions_api_client_qt.cpp extensions/extensions_api_client_qt.h
                extensions/extensions_browser_client_qt.cpp extensions/extensions_browser_client_qt.h
                extensions/extension_manager.cpp extensions/extension_manager.h
                extensions/extension_loader.cpp extensions/extension_loader.h
                extensions/extension_installer.cpp extensions/extension_installer.h
                extensions/file_system_delegate_qt.cpp extensions/file_system_delegate_qt.h
                extensions/messaging_delegate_qt.cpp extensions/messaging_delegate_qt.h
                extensions/mime_handler_view_guest_delegate_qt.cpp extensions/mime_handler_view_guest_delegate_qt.h
                extensions/plugin_info_host_qt.cpp extensions/plugin_info_host_qt.h
                extensions/plugin_service_filter_qt.cpp extensions/plugin_service_filter_qt.h
                extensions/unpacked_extension_installer.h extensions/unpacked_extension_installer.cpp
                net/plugin_response_interceptor_url_loader_throttle.cpp net/plugin_response_interceptor_url_loader_throttle.h
                renderer/extensions/extensions_renderer_api_provider_qt.cpp renderer/extensions/extensions_renderer_api_provider_qt.h
                renderer/extensions/extensions_renderer_client_qt.cpp renderer/extensions/extensions_renderer_client_qt.h
                renderer/extensions/resource_request_policy_qt.cpp renderer/extensions/resource_request_policy_qt.h
                extensions/webui/extensions_ui_page_handler_qt.cpp extensions/webui/extensions_ui_page_handler_qt.h
                extensions/webui/extensions_ui_qt.cpp extensions/webui/extensions_ui_qt.h
                extensions/webui/select_file_dialog.cpp extensions/webui/select_file_dialog.h
        )

        extend_gn_target(${buildGn} CONDITION QT_FEATURE_webengine_extensions AND QT_FEATURE_webengine_printing_and_pdf
            SOURCES
                extensions/pdf_iframe_navigation_throttle_qt.cpp extensions/pdf_iframe_navigation_throttle_qt.h
                printing/pdf_stream_delegate_qt.cpp printing/pdf_stream_delegate_qt.h
                printing/pdf_document_helper_client_qt.cpp printing/pdf_document_helper_client_qt.h
                renderer/plugins/loadable_plugin_placeholder_qt.cpp renderer/plugins/loadable_plugin_placeholder_qt.h
        )

        extend_gn_target(${buildGn} CONDITION WIN32
            SOURCES
                clipboard_util_win.cpp
                compositor/native_skia_output_device_direct3d11.cpp compositor/native_skia_output_device_direct3d11.h
        )

        extend_gn_target(${buildGn} CONDITION WIN32 AND QT_FEATURE_opengl
            SOURCES
                compositor/wgl_helper.cpp compositor/wgl_helper.h
        )

        ##
        #   GN PARAMETERS SETUP
        ##

        unset(gnArgArg)

        append_build_type_setup(gnArgArg)
        append_compiler_linker_sdk_setup(gnArgArg)
        append_sanitizer_setup(gnArgArg)
        append_toolchain_setup(gnArgArg)
        append_pkg_config_setup(gnArgArg)

        list(APPEND gnArgArg
            qtwebengine_target="${buildDir}/${config}/${arch}:QtWebEngineCore"
            build_dawn_tests=false
            build_with_tflite_lib=false
            webnn_use_tflite=false
            is_qtwebengine=true
            is_qtpdf=false
            chrome_pgo_phase=0
            disable_fieldtrial_testing_config=true
            enable_background_contents=false
            enable_background_mode=false
            enable_bluetooth_emulation=false
            enable_ipc_fuzzer=false
            enable_ipc_logging=false
            enable_java_templates=false
            enable_library_cdms=true
            enable_media_remoting=false
            enable_message_center=false
            enable_nacl=false
            enable_oop_printing=false
            enable_remoting=false
            enable_reporting=false
            enable_resource_allowlist_generation=false
            enable_screen_ai_service=false
            enable_session_service=false
            enable_supervised_users=false
            enable_swiftshader=false
            enable_swiftshader_vulkan=false
            enable_nocompile_tests=false
            angle_enable_swiftshader=false
            dawn_use_swiftshader=false
            pdf_enable_fontations=false
            enable_vr=false
            enable_web_speech=false
            enable_widevine=true
            enable_bound_session_credentials=false
            fatal_linker_warnings=false
            has_native_accessibility=false
            optimize_webui=false
            safe_browsing_mode=0
            strip_absolute_paths_from_debug_symbols=false
            toolkit_views=false
            pdf_use_skia=true
            use_cups=false
            use_dawn=false
            skia_use_dawn=false
            use_static_angle=true
            devtools_fast_bundle=false
            devtools_skip_typecheck=false
            headless_use_policy=false
        )
        extend_gn_list(gnArgArg
            ARGS use_v8_context_snapshot v8_use_external_startup_data
            CONDITION QT_FEATURE_webengine_v8_context_snapshot
        )
        extend_gn_list(gnArgArg
            ARGS enable_printing enable_print_preview enable_pdf
            CONDITION QT_FEATURE_webengine_printing_and_pdf
        )
        extend_gn_list(gnArgArg
            ARGS enable_plugins
            CONDITION QT_FEATURE_webengine_printing_and_pdf
        )
         extend_gn_list(gnArgArg
            ARGS enable_spellcheck
            CONDITION QT_FEATURE_webengine_spellchecker
        )
        extend_gn_list(gnArgArg
            ARGS enable_webrtc
            CONDITION QT_FEATURE_webengine_webrtc
        )
        extend_gn_list(gnArgArg
            ARGS enable_screen_capture
            CONDITION QT_FEATURE_webengine_webrtc
        )
        extend_gn_list(gnArgArg
            ARGS enable_hangout_services_extension
            CONDITION QT_FEATURE_webengine_webrtc AND QT_FEATURE_webengine_extensions
        )
        extend_gn_list(gnArgArg
            ARGS rtc_use_pipewire
            CONDITION QT_FEATURE_webengine_webrtc_pipewire
        )
        extend_gn_list(gnArgArg
            ARGS enable_extensions
            CONDITION QT_FEATURE_webengine_extensions
        )
        extend_gn_list(gnArgArg
            ARGS enable_vulkan
            CONDITION QT_FEATURE_webengine_vulkan
        )
        extend_gn_list(gnArgArg
            ARGS use_kerberos
            CONDITION QT_FEATURE_webengine_kerberos
        )
        extend_gn_list(gnArgArg
            ARGS proprietary_codecs rtc_use_h264
            CONDITION QT_FEATURE_webengine_proprietary_codecs
        )
        if(QT_FEATURE_webengine_proprietary_codecs)
            list(APPEND gnArgArg ffmpeg_branding="Chrome")
        endif()
        extend_gn_list(gnArgArg
            ARGS use_browser_spellchecker
            CONDITION QT_FEATURE_webengine_native_spellchecker
        )
        extend_gn_list(gnArgArg
            ARGS use_embedded_config
            CONDITION QT_FEATURE_webengine_embedded_build
        )
        extend_gn_list(gnArgArg
            ARGS enable_webenginedriver
            CONDITION QT_FEATURE_webenginedriver
        )

        if(LINUX)
            list(APPEND gnArgArg
                angle_enable_gl=true
                use_gtk=false # GTK toolkit bindings
                use_qt5=false # Qt5 toolkit bindings
                use_gio=false
                use_bundled_fontconfig=false
                use_glib=false
                use_bluez=false
                is_cfi=false
                use_ozone=true
                ozone_auto_platforms=false
                ozone_platform_headless=false
                ozone_platform_external=true
                ozone_platform="qt"
                ozone_extra_path="${CMAKE_CURRENT_LIST_DIR}/ozone/ozone_extra.gni"
            )
            set(systemLibs libjpeg libpng freetype harfbuzz libwebp libxml
                opus snappy icu ffmpeg re2 lcms2 libopenjpeg2 libvpx
            )
            foreach(slib ${systemLibs})
                extend_gn_list(gnArgArg
                    ARGS use_system_${slib}
                    CONDITION QT_FEATURE_webengine_system_${slib}
                )
            endforeach()
            if(NOT QT_FEATURE_webengine_system_opus)
                extend_gn_list(gnArgArg
                   ARGS has_perl
                   CONDITION Perl_FOUND
            )
            endif()
            extend_gn_list(gnArgArg
                ARGS use_system_libxslt
                CONDITION QT_FEATURE_webengine_system_libxml
            )
            extend_gn_list(gnArgArg
                ARGS icu_use_data_file
                CONDITION NOT QT_FEATURE_webengine_system_icu
            )
            extend_gn_list(gnArgArg
                ARGS use_system_zlib use_system_minizip
                CONDITION QT_FEATURE_webengine_system_zlib AND QT_FEATURE_webengine_system_minizip
            )
            extend_gn_list(gnArgArg
                ARGS pdfium_use_system_zlib
                CONDITION QT_FEATURE_webengine_system_zlib
            )
            extend_gn_list(gnArgArg
                ARGS pdfium_use_system_libpng
                CONDITION QT_FEATURE_webengine_system_libpng
            )
            extend_gn_list(gnArgArg
                ARGS pdfium_use_system_libtiff
                CONDITION QT_FEATURE_webengine_system_libtiff
            )
            extend_gn_list(gnArgArg
                ARGS use_libpci
                CONDITION QT_FEATURE_webengine_system_libpci
            )
            extend_gn_list(gnArgArg
                ARGS use_alsa
                CONDITION QT_FEATURE_webengine_system_alsa
            )
            extend_gn_list(gnArgArg
                ARGS use_pulseaudio
                CONDITION QT_FEATURE_webengine_system_pulseaudio
            )
            extend_gn_list(gnArgArg
                ARGS use_system_minigbm
                CONDITION QT_FEATURE_webengine_system_gbm
            )
            extend_gn_list(gnArgArg
                ARGS rtc_system_openh264
                CONDITION QT_FEATURE_webengine_webrtc_system_openh264
            )
            extend_gn_list(gnArgArg
                ARGS use_vaapi
                CONDITION QT_FEATURE_webengine_vaapi
            )
            extend_gn_list(gnArgArg
                ARGS ozone_platform_x11 use_xkbcommon
                CONDITION QT_FEATURE_webengine_ozone_x11
            )
            extend_gn_list(gnArgArg
                ARGS rtc_use_x11
                CONDITION QT_FEATURE_webengine_ozone_x11 AND QT_FEATURE_webengine_webrtc
            )
            extend_gn_list(gnArgArg
                ARGS use_udev
                CONDITION QT_FEATURE_webengine_system_libudev
            )

            if(QT_FEATURE_webengine_kerberos)
                list(APPEND gnArgArg
                     external_gssapi_include_dir="${GSSAPI_INCLUDE_DIRS}/gssapi"
                )
            endif()
            get_gn_arch(cpu ${TEST_architecture_arch})

            if(cpu STREQUAL "arm" AND
               CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND
               CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11.0.0")
                list(APPEND gnArgArg libyuv_use_neon=false)
                #TODO check if libyuv really uses udot in case of arm32
            endif()
            if(cpu STREQUAL "arm64" AND NOT QT_FEATURE_webengine_arm64_udot_support)
                list(APPEND gnArgArg libyuv_use_neon=false)
            endif()
            if(cpu STREQUAL "arm")
                check_thumb(armThumb)
                if(NOT armThumb AND NOT QT_FEATURE_webengine_system_ffmpeg)
                    list(APPEND gnArgArg media_use_ffmpeg=false use_webaudio_ffmpeg=false)
                endif()
                # sme and sve depend on armv9
                list(APPEND gnArgArg libyuv_use_sme=false
                                     libyuv_use_sve=false)
            endif()

            if(CMAKE_CROSSCOMPILING AND cpu STREQUAL "arm64")
                # This is a workaround to avoid auto test timeouts on the QEMU arm64 CI.
                if ("$ENV{TARGET_OSVERSION_COIN}" STREQUAL "qemu")
                    list(APPEND gnArgArg
                        v8_enable_sandbox=false
                        arm_control_flow_integrity="none"
                    )
                endif()
            endif()
            if(cpu STREQUAL "arm64")
                if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "14.0.0")
                    list(APPEND gnArgArg libyuv_use_sme=false)
                endif()
                if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0.0")
                    list(APPEND gnArgArg libyuv_use_sve=false)
                endif()
            endif()
            unset(cpu)
        endif()

        if(MACOS)
            list(APPEND gnArgArg
                use_external_popup_menu=false
                skia_use_metal=false
            )
        endif()

        if(WIN32)
            list(APPEND gnArgArg
                com_init_check_hook_disabled=true
                heterogeneous_executables=true
            )
        endif()

        add_gn_command(
            CMAKE_TARGET WebEngineCore
            NINJA_TARGETS QtWebEngineCore convert_dict
            GN_TARGET ${buildGn}
            GN_ARGS ${gnArgArg}
            BUILDDIR ${buildDir}/${config}/${arch}
            MODULE core
        )

    endforeach()
    create_cxx_configs(WebEngineCore ${arch})
endforeach()


##
#  WEBENGINECORE SETUP
##

list(GET archs 0 arch)
target_include_directories(WebEngineCore PRIVATE
    ${buildDir}/$<CONFIG>/${arch}/gen
    ${buildDir}/$<CONFIG>/${arch}/gen/third_party/perfetto
    ${buildDir}/$<CONFIG>/${arch}/gen/third_party/perfetto/build_config
    ${buildDir}/$<CONFIG>/${arch}/gen/base/allocator/partition_allocator/src
)

set(stamps QtWebEngineCore.stamp)
set(dataStampOption "")
if(QT_FEATURE_webengine_v8_context_snapshot)
    set(dataStampOption NINJA_DATA_STAMP obj/tools/v8_context_snapshot/v8_context_snapshot.stamp)
endif()

add_gn_build_artifacts_to_target(
    CMAKE_TARGET WebEngineCore
    NINJA_TARGET QtWebEngineCore
    MODULE core
    DEPENDS WebEngineCore_sync_headers
    BUILDDIR ${buildDir}
    COMPLETE_STATIC FALSE
    NINJA_STAMP QtWebEngineCore.stamp
    ${dataStampOption}
)
add_dependencies(WebEngineCore run_core_NinjaDone)
if(COIN_BUG_699)
   set_property(TARGET WebEngineCore PROPERTY CXX_LINKER_LAUNCHER ${PROJECT_BINARY_DIR}/linker_ulimit.sh)
endif()

##
#  SANDBOX
##

if(WIN32)
    add_library(WebEngineCoreSandbox STATIC IMPORTED GLOBAL)
    foreach(config ${configs})
        string(TOUPPER ${config} cfg)
        add_ninja_command(
            TARGET QtWebEngineCoreSandbox
            OUTPUT QtWebEngineCoreSandbox.lib
            BUILDDIR ${buildDir}/${config}/${arch}
            DEPENDS WebEngineCore_sync_headers
            MODULE core
        )
        qt_webengine_add_gn_target_to_sbom(QtWebEngineCoreSandbox ${buildDir}/${config}/${arch})
        set(sandboxLibraryPath ${buildDir}/${config}/${arch}/QtWebEngineCoreSandbox.lib)
        set_property(TARGET WebEngineCoreSandbox
            PROPERTY IMPORTED_LOCATION_${cfg} ${sandboxLibraryPath}
        )
        add_custom_target(sandboxLibrary_${config}
            DEPENDS ${buildDir}/${config}/${arch}/QtWebEngineCoreSandbox.lib)
        add_dependencies(run_core_NinjaDone sandboxLibrary_${config})
        add_dependencies(WebEngineCoreSandbox WebEngineCore)
    endforeach()
endif()

##
#  WEBENGINECORE DICT TOOL SETUP
##

if(QT_FEATURE_webengine_spellchecker AND NOT CMAKE_CROSSCOMPILING)
    qt_get_tool_target_name(dict_target_name qwebengine_convert_dict)
    target_include_directories(${dict_target_name} PRIVATE
        ../3rdparty/chromium
        ../3rdparty/chromium/third_party/boringssl/src/include
        ../3rdparty/chromium/base/allocator/partition_allocator/src
        ${buildDir}/$<CONFIG>/${arch}/gen
        ${buildDir}/$<CONFIG>/${arch}/gen/base/allocator/partition_allocator/src
    )
    add_gn_build_artifacts_to_target(
        CMAKE_TARGET ${dict_target_name}
        NINJA_TARGET convert_dict
        MODULE core
        BUILDDIR ${buildDir}
        COMPLETE_STATIC FALSE
        NINJA_STAMP convert_dict.stamp
    )
    add_dependencies(${dict_target_name} run_core_NinjaDone)
    add_dependencies(${dict_target_name} WebEngineCore)
endif()

##
#  WEBENGINEDRIVER
##

if(QT_FEATURE_webenginedriver)
    add_ninja_command(
        TARGET webenginedriver_group
        OUTPUT ${WEBENGINEDRIVER_EXECUTABLE}
        BUILDDIR ${buildDir}/$<CONFIG>/${arch}
        MODULE core
    )
    qt_webengine_add_gn_target_to_sbom(webenginedriver_group ${buildDir}/$<CONFIG>/${arch})
    add_custom_target(webenginedriver
        DEPENDS
            ${buildDir}/$<CONFIG>/${arch}/${WEBENGINEDRIVER_EXECUTABLE})
    add_dependencies(run_core_NinjaDone webenginedriver)
endif()

##
#  CHROMIUM UPDATE
##

add_custom_target(update-chromium
    COMMAND ${CMAKE_COMMAND} -P ${WEBENGINE_ROOT_SOURCE_DIR}/cmake/SubmoduleUpdate.cmake
    DEPENDS ${WEBENGINE_ROOT_SOURCE_DIR}/cmake/SubmoduleUpdate.cmake
)
